﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WMSSyncData.Model.WMSModel;
using WMSSyncData.Utils;

namespace WMSSyncData.DAL.SqlDB
{
    public class WMS_T_OutStockRowsDAL : BaseSqlDBDAL<WMS_T_OutStockRows>
    {
        public WMS_T_OutStockRows Get(string relationBill, string sn, string rowNo)
        {
            var sql = string.Format(@"SELECT * FROM dbo.wms_T_OutStockRows WHERE BillNo IN (
SELECT BillNo FROM dbo.wms_T_OutStock 
WHERE RelatedBill='{0}' AND SN='{1}' AND RowNo='{2}' and status='0') AND RowNo='{2}' AND SN='{1}' AND ISNULL(SelectQty,0)=0", relationBill, sn, rowNo);

            var dt = SqlDBHelper.GetDataSet(sql).Tables[0];

            var list = MakeTablePackage(dt);

            var result = list.FirstOrDefault();

            return result;
        }

        public bool UpdateOutStockStatus(string billNo)
        {
            var result = false;
            var sql = string.Format(@"UPDATE dbo.wms_T_OutStock SET Status='D' WHERE BillNo='{0}' AND Status='0'", billNo);

            var res = SqlDBHelper.ExcuteSQL(sql);

            if (res > 0)
            {
                result = true;
            }

            return result;
        }

        public bool UpdateOutStockRowsPlanQty(string billNo, int planQty, string remark)
        {
            var result = false;

            var sql = string.Format(@"UPDATE dbo.wms_T_OutStock SET Remark='{2}' WHERE BillNo='{1}';
UPDATE dbo.wms_T_OutStockRows SET PlanQty={0} WHERE BillNo='{1}' AND ISNULL(SelectQty,0)=0", planQty, billNo, remark);
            var res = SqlDBHelper.ExcuteSQL(sql);
            if (res > 0)
            {
                result = true;
            }
            return result;
        }



    }
}
